package com.freedom.start.common.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 处理固定url的请求的参数
 * @Author: zhenggang.liu
 * @Date: 2020/2/10 14:23
 */
public class ParamsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestURI = httpRequest.getRequestURI();
        // 路径结尾如果是save，则使用自定义的过滤器对字段进行处理
        if (requestURI.endsWith("save")) {
            ParameterRequestWrapper parameterRequestWrapper = new ParameterRequestWrapper(httpRequest);
            chain.doFilter(parameterRequestWrapper, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
